//
//  Common.m
//  ProcessTimeBudget
//
//  Created by isempty.chiu on 2010/9/13.
//  Copyright 2010 LifeMotion. All rights reserved.
//

#import "CommonFunc.h"


@implementation CommonFunc
+ (NSString*) getStringFromTimeInterval: (NSTimeInterval) interval
{
	NSInteger min;
	NSInteger sec;
	NSInteger hour;
	NSInteger day;
	
	day = floor(interval/86400);
	hour = floor((interval - day * 86400) / 3600);
	min = floor((interval - day * 86400 - hour * 3600) / 60);
	sec = round(interval - day * 86400 - hour * 3600 - min * 60);
	
	if(day > 0)
		return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:%d:%d:%d", day, hour, min, sec]];
	else if(hour > 0)
	{
		if(min > 9 && sec > 9)
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:%d:%d", hour, min, sec]];
		else if(min > 9 && sec < 10)
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:%d:0%d", hour, min, sec]];
		else if(min < 10 && sec > 9)
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:0%d:%d", hour, min, sec]];
		else
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:0%d:0%d", hour, min, sec]];
	}
	else if(min > 0)
	{
		if(sec > 9)
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:%d", min, sec]];
		else
			return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d:0%d", min, sec]];
	}
	else
		return [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d", sec]];
}

//"yyyy-MM-dd HH:mm:ss ZZZ"
+ (NSString*) getDateString:(NSDate*)date byFormat:(NSString*)format
{
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:format];
	
	//NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
	//[dateFormatter setLocale:usLocale];	
	//[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"en_US"]];
	NSString *stringFromDate = [dateFormatter stringFromDate:date];
	[dateFormatter release];
	//[usLocale release];
	
	return stringFromDate;
}
@end
